<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Barang
 *
 * @author fendi
 */
class barang extends My_Controller{
    function barang(){
        parent::My_Controller();
        $this->data['title']='Manajemen Barang';
        $this->load->model('m_barang');
        $this->load->model('m_kategori');
    }
    var $data;
    //menampilkan daftar barang
    function index(){
        $this->data['main_view']='barang/view_barang';
        $this->data['listBarang']=$this->m_barang->getAllBarang();
        $this->data['listKategori']=$this->m_kategori->getAllKategori();
        $this->render($this->data);
    }
    function viewByKategori($kategori=''){
        $this->data['main_view']='barang/view_barang';
        //untuk post
        if($kategori==''){
            if($_POST['kategori']=='kosong'){
                redirect('barang/');
            }else
                $this->data['kategoriSelected']=$_POST['kategori'];
        }
        else{
            //untuk get
            $this->data['kategoriSelected']=$kategori;
        }
        
        $this->data['listBarang']=$this->m_barang->getByKategori($this->data['kategoriSelected']);
        $this->data['listKategori']=$this->m_kategori->getAllKategori();
        $this->render($this->data);
    }
    function addBarang_form(){
        $this->data['main_view']='barang/addBarang_form';
        $this->data['subLink']='barang/addBarang_proses';
        $this->data['listKategori']=$this->m_kategori->getAllKategori();
        $this->render($this->data);
    }
    function addBarang_proses(){
        $affect=$this->m_barang->insertBarang($_POST);
        if($affect>0){
            $this->data['message']='Data berhasil dimasukkan';
        }else
            $this->data['message']='Data gagal dimasukkan';
        
        $this->viewByKategori($_POST['id_kategori']);
    }
    function editBarang_form($id){
        $this->data['main_view']='barang/editBarang_form';
        $this->data['subLink']='barang/editBarang_proses';
        $this->data['barang']=$this->m_barang->getByIdBarang($id);
        $this->data['listKategori']=$this->m_kategori->getAllKategori();
        $this->render($this->data);
    }
    function editBarang_proses(){
        $affected=$this->m_barang->updateBarang($_POST);
        if($affected>0)
            $this->data['message']=$_POST['nama_barang'].' berhasil di update';
        else
            $this->data['message']=$_POST['nama_barang'].' gagal di update';
        $this->viewByKategori($_POST['id_kategori']);
    }

    function getAutocomplateData(){
        //$barang=$this->m_barang->getBarangByNama($_GET['q']);
        $barang=$this->m_barang->getAllBarang();
        die(json_encode($barang));
    }
}
?>